﻿using System;
using System.Linq;
using System.Collections.Generic;

using SimpleForum.Data.Models;
using SimpleForum.Data.Repositories;

namespace SimpleForum.Data.Services
{
    public class TagService : ITagService
    {
        private readonly IRepository<Tag> TagRepository;

        public TagService(IRepository<Tag> TagRepository)
        {
            this.TagRepository = TagRepository;
        }

        public Tag Get(string Name)
        {
            return TagRepository.Get().SingleOrDefault(x => x.Name == Name) ?? null;
        }

        public IEnumerable<Tag> GetTags()
        {
            return TagRepository.Get();
        }

        public void AddTagsToThread(Thread Thread, string Tags)
        {
            foreach (string Tag in Tags.Trim().Split(' ').GroupBy(x => x).Select(x => x.Key))
            {
                Thread.Tags.Add(new Tag
                {
                    Name = Tag
                });
            }
        }
    }
}